package com.focus.xyz.union.pages.frontend.forms;

import com.focus.xyz.core.common.generator.DriverFactory;
import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseForm;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.InsPlanBean;
import com.focus.xyz.union.pages.frontend.DestinationPage;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by cimon on 2016/5/7.
 */
public class InsPlanForm extends BaseForm {
    private Action action;
    private static final String NODE_XPATH = "tbody/tr/td/select|tbody/tr/td/input";
    private WebDriver driver;


    @FindBy(id = "dev_targetPlace")
    private WebElement destination;
    @FindBy(id = "dev_startTime")
    private WebElement beginDate;
    @FindBy(id = "dev_endTime")
    private WebElement endDate;

    @FindAll(
            {@FindBy(id = "dev_period"),@FindBy(id = "dev_startTime"),@FindBy(id = "dev_endTime")}
    )
    private List<WebElement> planList;

    public InsPlanForm(WebDriver driver, Action action){
        super(driver,action);
        this.action = action;
        this.driver = driver;
        PageFactory.initElements(this.driver,this);
    }

    @Override
    protected List<WebElement> getAllElements() throws Exception {
        return planList;
    }

    public void clickDestBtn() throws Exception{
        if(action.isElementExsit(By.id("dev_targetPlace"))){
            action.click(destination);
        }
    }

    public void inputBeginDate(String begin) throws Exception{
        action.inputText(beginDate,begin);
    }

    public void inputEndDate(String end) throws Exception{
        action.inputText(endDate,end);
    }

    public void esc() throws Exception{
        action.esc();
    }


    /*填写投保计划的form*/
    public void fillInsPlan(String insPlanId) throws Exception{
        if(StringUtils.isNotBlank(insPlanId)){
            InsPlanBean insPlanBean = InsDataService.getInsPlanById(insPlanId);
            if(StringUtils.isNotBlank(insPlanBean.getDestination())){
                action.pause(2000l);
                clickDestBtn();
                action.pause(2000l);
                DestinationPage dp = new DestinationPage(driver,action);
                dp.popDestDiv();
                dp.selectDestination(insPlanBean.getDestination());
            }
            if(insPlanBean == null){
                throw new Exception("insPlan 为空");
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dbDate = DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getSysDate();
            int i = DateUtils.truncatedCompareTo(sdf.parse(dbDate),sdf.parse(insPlanBean.getDev_startTime()),9);
            if(i >= 0){
                insPlanBean.setDev_startTime(DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getNextDate(1));
            }
            i = DateUtils.truncatedCompareTo(sdf.parse(dbDate),sdf.parse(insPlanBean.getDev_endTime()),9);
            if(i >= 0){
                insPlanBean.setDev_endTime(DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getNextDate(1));
            }
            bind(insPlanBean);
            esc();
        }
    }
}
